import csv

import os
import pathlib

csv_path = pathlib.Path(os.path.join(os.path.dirname(__file__),"simple2.csv"))


def writecsv()->None:
    headers = ['Symbol','Price','Date','Time'
               'change','Volume']
    lines =[
        ["AA",39.48,"6/11/2007","9:36am",-0.18,181800],
        ["AIG",71.38,"6/11/2007","9:36am",-0.15,195500],
        ["AXP",62.58,"6/11/2007","9:36am",-0.46,935000],
        ["BA",98.31,"6/11/2007","9:36am",+0.12,104800],
        ["C",53.08,"6/11/2007","9:36am",-0.25,360900],
        ["CAT",78.29,"6/11/2007","9:36am",-0.23,225400],
    ]
    with open('simple.csv','w',newline='') as f:
        w = csv.writer(f)
        w.writerow(headers)
        w.writerows(lines)

    with csv_path.open("w",newline="",encoding="utf-8") as f:
        w = csv.writer(f)
        w.writerow(headers)
        w.writerows(lines)

def readcsv()->None:
    with open('simple.csv','r') as f:
        r = csv.reader(f)
        for i,row in enumerate(r):
            print(i,"---",len(row),row)

def writedict():
    headers = ['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume']
    rows = [{'Symbol':'AA', 'Price':39.48, 'Date':'6/11/2007',
            'Time':'9:36am', 'Change':-0.18, 'Volume':181800},
            {'Symbol':'AIG', 'Price': 71.38, 'Date':'6/11/2007',
            'Time':'9:36am', 'Change':-0.15, 'Volume': 195500},
            {'Symbol':'AXP', 'Price': 62.58, 'Date':'6/11/2007',
            'Time':'9:36am', 'Change':-0.46, 'Volume': 935000},
            ]

    with open('stocks.csv','w',newline="") as f:
        f_csv = csv.DictWriter(f, headers)
        f_csv.writeheader()
        f_csv.writerows(rows)

def readdict():
    with open('simple.csv','r') as f:
        f_csv = csv.DictReader(f)

        for i,row in enumerate(f_csv):
            print(i,row)

if __name__ == "__main__":
    #writecsv()
    #readcsv()

    writedict()
    readdict()